package com.ifcar.app.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ifcar.app.api.dao.PublishApi;
import com.ifcar.app.api.dao.impl.PublishApiImpl;
import com.ifcar.app.module.PublishMsg;
import com.ifcar.app.module.Question;
import com.ifcar.app.util.EC;
import com.url.ajax.json.JSONException;
import com.url.ajax.json.JSONObject;

/**
 * 信息发布
 * @author nxlixa
 *
 */
public class Publish extends Base {
	
	private static final long serialVersionUID = -8169005199342450467L;

	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		req.setCharacterEncoding("UTF-8");
		String content = "";
		String method = req.getParameter("method");
		
		if ("findc".equals(method)) { // find car
			content = publishFindCar(req,resp);
			sendContent(resp, content, CONTEXT_TYPE);
		}else if("findp".equals(method)){ // find people
			content = publishFindPeople(req,resp);
			sendContent(resp, content, CONTEXT_TYPE);
		}else if("question".equals(method)){ // publish question
			content = publishQuestion(req,resp);
			sendContent(resp, content, CONTEXT_TYPE);
		}else if("answer".equals(method)){ // answer question
			content = publishAnswer4Question(req,resp);
			sendContent(resp, content, CONTEXT_TYPE);
		}else{
			
			_404Content(resp);
			
		}
		
		return;
	}
	
	/**
	 * 发布信息 求车
	 * @param request
	 * @param response
	 * @return
	 */
	private String publishFindCar(ServletRequest request, ServletResponse response){
		
		PublishMsg pubObj = new PublishMsg();

		pubObj.isOnwer = request.getParameter("isonwer");
		pubObj.account = request.getParameter("account");
		pubObj.location = request.getParameter("location");
		pubObj.begin = request.getParameter("begin");
		pubObj.end = request.getParameter("end");
		pubObj.price = request.getParameter("price");
		pubObj.pnumber = request.getParameter("pnumber");
		pubObj.center = request.getParameter("center");
		pubObj.iscircle = request.getParameter("iscircle");
		pubObj.stime = request.getParameter("stime");
		pubObj.rtime = request.getParameter("rtime");
		pubObj.digest = request.getParameter("digest");
		
		PublishApi dao = new PublishApiImpl();
		JSONObject message = null;
		try {
			message = dao.publishMsgFindCar(pubObj);
		} catch (Exception e) {
			try {
				message.remove(EC.SUCCESS);
				message.put(EC.CODE, EC.CODE_SERVER_ERROR);
				message.put(EC.ERROR, EC.MSG_SERVER_ERROR);
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
		return  message.toString();	
	}
	
	/**
	 * 发布信息 求客
	 * @param request
	 * @param response
	 * @return
	 */
	private String publishFindPeople(ServletRequest request, ServletResponse response){
		
		PublishMsg pubObj = new PublishMsg();
		
		pubObj.isOnwer = request.getParameter("isonwer");
		pubObj.account = request.getParameter("account");
		pubObj.location = request.getParameter("location");
		pubObj.begin = request.getParameter("begin");
		pubObj.end = request.getParameter("end");
		pubObj.price = request.getParameter("price");
		pubObj.pnumber = request.getParameter("pnumber");
		pubObj.center = request.getParameter("center");
		pubObj.iscircle = request.getParameter("iscircle");
		pubObj.stime = request.getParameter("stime");
		pubObj.rtime = request.getParameter("rtime");
		pubObj.cartype = request.getParameter("cartype");
		pubObj.carcode = request.getParameter("carcode");
		pubObj.digest = request.getParameter("digest");
		
		PublishApi dao = new PublishApiImpl();
		JSONObject message = null;
		try {
			message = dao.publishMsgFindPeople(pubObj);
		} catch (Exception e) {
			try {
				message.remove(EC.SUCCESS);
				message.put(EC.CODE, EC.CODE_SERVER_ERROR);
				message.put(EC.ERROR, EC.MSG_SERVER_ERROR);
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
		return  message.toString();
	}
	
	/**
	 * 发布问题
	 * @param request
	 * @param response
	 * @return
	 */
	private String publishQuestion(ServletRequest request, ServletResponse response){
		
		Question qu = new Question();
		
		qu.account = request.getParameter("account");
		qu.location = request.getParameter("location");
		qu.locationName = request.getParameter("locationname");
		qu.questionContent = request.getParameter("content");
		
		PublishApi dao = new PublishApiImpl();
		JSONObject message = null;
		try {
			message = dao.publishQuestioin(qu);
		} catch (Exception e) {
			try {
				message.remove(EC.SUCCESS);
				message.put(EC.CODE, EC.CODE_SERVER_ERROR);
				message.put(EC.ERROR, EC.MSG_SERVER_ERROR);
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
		return  message.toString();
	}
	
	/**
	 * 回答问题
	 * @param request
	 * @param response
	 * @return
	 */
	private String publishAnswer4Question(ServletRequest request, ServletResponse response){
		
		Question qu = new Question();
		
		qu.questionId = request.getParameter("qid");
		qu.account = request.getParameter("account");
		qu.location = request.getParameter("location");
		qu.locationName = request.getParameter("locationname");
		qu.questionContent = request.getParameter("answer");
		
		PublishApi dao = new PublishApiImpl();
		JSONObject message = null;
		try {
			message = dao.publishAnswer4Questioin(qu);
		} catch (Exception e) {
			try {
				message.remove(EC.SUCCESS);
				message.put(EC.CODE, EC.CODE_SERVER_ERROR);
				message.put(EC.ERROR, EC.MSG_SERVER_ERROR);
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		
		return  message.toString();
	}

}
